﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CAN_Monitor
{
    public partial class frmValueWatch : Form
    {
        #region ctor

        public frmValueWatch(frmMain parent)
        {
            InitializeComponent();
            mParentWindow = parent;

            this.timer1.Start();
        }

        #endregion

        #region Members

        private frmMain mParentWindow = null;
        //private List<CANLib.CANPacket> mChangeList = new List<CANLib.CANPacket>();
        private List<CANLib.CANPacket> mWatchList = new List<CANLib.CANPacket>();
        //private Dictionary<ushort, byte[]> mWatchList = new Dictionary<ushort, byte[]>();
        //private Dictionary<ushort, ValueFormat> mValueFormats = new Dictionary<ushort, ValueFormat>();

        #endregion

        #region Properties

        #endregion

        #region Methods

        public void UpdateValue(CANLib.CANPacket packet)
        {
            lock (mWatchList)
            {
                if (mWatchList.Contains(packet))
                {
                    mWatchList[mWatchList.IndexOf(packet)] = packet;
                }
            }
        }

        //----------------------------------------------------------------------------------------

        public void UpdateValue(CANLib.CANPacket[] packets)
        {
            lock (mWatchList)
            {
                foreach (CANLib.CANPacket packet in packets)
                {
                    if (mWatchList.Contains(packet))
                    {
                        mWatchList[mWatchList.IndexOf(packet)] = packet;
                    }
                }//foreach
            }
        }

        //----------------------------------------------------------------------------------------

        private string BuildFilterString()
        {
            string filterStr = "";

            //Build filter with all IDs
            foreach (CANLib.CANPacket packet in this.mWatchList)
            {
                filterStr += "ID==" + packet.ID.ToString() + "||";
            }

            filterStr = filterStr.TrimEnd('|');

            return filterStr;
        }

        //----------------------------------------------------------------------------------------

        #endregion

        #region Events

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ushort newId = 0;

            //Add
            if(ushort.TryParse(txtAdd.Text, out newId))
            {
                if (!this.mWatchList.Contains(new CANLib.CANPacket(newId, 0, null)))
                {
                    this.mWatchList.Add(new CANLib.CANPacket(newId, 0, null));
                }
            }

            //Set string in main window so we can get packets
            this.mParentWindow.WatchFilterString = this.BuildFilterString();
        }

        //----------------------------------------------------------------------------------------

        private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (ltbWatchList.SelectedIndex >= 0)
            {
                switch (e.ClickedItem.Text)
                {
                    case "Unknown":
                        this.mWatchList[ltbWatchList.SelectedIndex].Format = CANLib.ValueFormat.FMT_UNKNOWN;
                        break;
                    case "Integer":
                        this.mWatchList[ltbWatchList.SelectedIndex].Format = CANLib.ValueFormat.FMT_INTEGER;
                        break;
                    case "Double":
                        this.mWatchList[ltbWatchList.SelectedIndex].Format = CANLib.ValueFormat.FMT_DOUBLE;
                        break;
                    case "String":
                        this.mWatchList[ltbWatchList.SelectedIndex].Format = CANLib.ValueFormat.FMT_STRING;
                        break;
                }
            }
        }

        //----------------------------------------------------------------------------------------

        private void timer1_Tick(object sender, EventArgs e)
        {
            int oldSelectedIndex = ltbWatchList.SelectedIndex;

            //Delete old items
            this.ltbWatchList.Items.Clear();

            lock (mWatchList)
            {
                foreach (CANLib.CANPacket packet in mWatchList)
                {
                    //Add this string to the watch list
                    this.ltbWatchList.Items.Add(packet.ToString());
                }//foreach
            }//lock(mWatchlist)

            //Reselect after changing list
            if (oldSelectedIndex >= 0)
            {
                ltbWatchList.SelectedIndex = oldSelectedIndex;
            }

        }//timer1_Tick

        //----------------------------------------------------------------------------------------

        private void frmValueWatch_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.timer1.Stop();
        }

        //----------------------------------------------------------------------------------------

        #endregion
    }
}
